package com.riverstar.wwndemo.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.RootPanel;


public class WWNDemo implements EntryPoint, ResizeHandler {
	private static WWNDemo singleton;
	private static final Images images = GWT.create(Images.class);
	
	public interface Images extends TopPanel.Images, CenterPanel.Images {
	}
	
	/**
	 * Gets the singleton WWNDemo instance.
	 */
	public static WWNDemo get() {
		return singleton;
	}
	
	private TopPanel topPanel = new TopPanel(images);
	private CenterPanel centerPanel = new CenterPanel(images);
	private BottomPanel bottomPanel = new BottomPanel(images);
	
	public void onModuleLoad() {
	    DockPanel outer = new DockPanel();
	    outer.add(topPanel, DockPanel.NORTH);
	    outer.add(centerPanel, DockPanel.CENTER);
	    outer.add(bottomPanel, DockPanel.SOUTH);
	    
	    topPanel.setWidth("100%");
	    centerPanel.setWidth("100%");
	    bottomPanel.setWidth("100%");
	    
	    Window.addResizeHandler(this);
	    //Window.enableScrolling(false);
	    Window.setMargin("0px");
	    RootPanel.get().add(outer);

	    DeferredCommand.addCommand(new Command() {
	    	public void execute() {
	    		onWindowResized(Window.getClientWidth(), Window.getClientHeight());
	    	}
	    });

	    onWindowResized(Window.getClientWidth(), Window.getClientHeight());
	}

	public void onResize(ResizeEvent event) {
		onWindowResized(event.getWidth(), event.getHeight());
	}

	public void onWindowResized(int width, int height) {
		topPanel.adjustSize(width, height);
		bottomPanel.adjustSize(width, height);
		centerPanel.adjustSize(width, height - topPanel.getHeight() - bottomPanel.getHeight() );
	}
}